package com.gec.controller;

import com.gec.domain.Dept;
import com.gec.domain.PageBean;
import com.gec.domain.PageParam;
import com.gec.domain.Room;
import com.gec.service.abst.DeptService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/Dept")
public class DeptController extends BaseController {
    private final DeptService deptService;
    private final String Json = "application/json;charset=UTF-8";

    public DeptController(DeptService deptService) {
        this.deptService = deptService;
    }

    @RequestMapping("/viewList")
    public String viewList() {
        return "dept/list";
    }


    @RequestMapping(value = "/jsonList",
            produces = Json)
    @ResponseBody
    public String jsonList(HttpServletRequest req) {
        //   获取分页相关信息, 搜索关键字。
        PageParam param = PageParam.getPageParam(req);
        PageBean<Room> pBean = null;
        try {
            pBean = deptService.getAllDept(param);
            return pBean.toJSON();
        } catch (Exception e) {
            e.printStackTrace();
            //{ps}你要新建一个方法到 PageBean
            return pBean.errorMsg();
        }
    }

    @RequestMapping("/viewAdd")
    public String viewAdd(@RequestParam("id") String id
            , Model model) {
        Dept dept;
        try {
            System.out.println(id);
            dept = deptService.getDeptById(id);
            System.out.println("+-------------------------------------+");
            System.out.println(dept);
            System.out.println("+-------------------------------------+");
            model.addAttribute("pDept", dept);
            return "dept/addDept";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "dept/addDept";
    }

    @RequestMapping(value = "/saveDept",
            produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String saveDept(Dept dept) {
        try {
            System.out.println("Dept:" + dept);
            deptService.saveDept(dept);
            System.out.println(sendSuccess());
            return sendSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return sendFailed(e);
        }

    }

    @RequestMapping(value = "/delete",
            produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String deleteDept(@RequestParam("id") String id) {
        try {
            System.out.println("id:" + id);
            deptService.deleteDept(id);
            return sendSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return sendFailed(e);
        }
    }

    @RequestMapping(value = "/getDeptById",
            produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String getDeptById(@RequestParam("id") String id) {
        Dept dept;
        try {
            dept = deptService.getDeptById(id);
            System.out.println(dept);
            return sendData(dept);
        } catch (Exception e) {
            e.printStackTrace();
            return sendFailed(e);
        }


    }
}
